<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Shop\Api\CategoryApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductGroupApi;
use Admin\Api\DatatreeApi;

class ProductGroupController extends AdminController{

	private $cate;

	public function index(){
		$id = I('id',0);
		if(IS_GET){

			$select_cate_parent = I('get.select_cate_parent','');

			if($select_cate_parent == ''){
				$select_cate_parent = 0;
			}
			$this -> assign('select_cate_parent',$select_cate_parent);

			$this -> cate = array();
			if($select_cate_parent != 0){
				$this->queryChild($select_cate_parent);
			}

			$store_id = I('get.store_id',0);
			$page_no = I('get.p',1);
			$this->assign("store_id",$store_id);

			$this -> assign('id',$id);

			$parent = getDatatree('WXPRODUCTGROUP');

			$result = apiCall(DatatreeApi::GET_INFO,array(array('id' => $id)));
			if($result['status']){
				if($result['info']['parentid'] != $parent){
					$this -> error("分类id参数错误！");
				}
				$this -> assign('name',$result['info']['name']);
			}else{
				$this -> error($result['info']);
			}
			//查询八个馆
			$result = apiCall(CategoryApi::QUERY_NO_PAGING,array(array('level'=>1,'parent'=>0)));
			if($result['status']){
				$this -> assign('cate_parent',$result['info']);
			}else{
				$this -> error($result['info']);
			}

			//查询该分类商品信息
			$map = array(
				'g.g_id' => $id,
				'p.status' => 1,
				'p.onshelf' => 1,
			);
			if($select_cate_parent != 0){
				$map['p.cate_id'] = array('in',$this->cate);
			}
			$page = array('curpage'=>$page_no,'size'=>10);
			$field = "g.id,g.g_id,g.start_time,g.end_time,g.display_order";
			$field = $field.",p.id as p_id,p.name";
			$order = "display_order desc";
			$result = apiCall(ProductGroupApi::QUERY_WITH_PRODUCT,array($map,$page,$order,false,$field));
			if(!$result['status']){
				$this->error($result['info']);
			}

			$this->assign("group",$result['info']['list']);

			$this -> assign("group_show",$result['info']['show']);

			/*$result = apiCall(ProductApi::QUERY_NO_PAGING_ON_SHELF,array());
			if($result['status']){
				$this -> assign('product',$result['info']);
			}else{
				$this -> error($result['info']);
			}*/

			$this->display();
		}
	}

	public function getProduct(){

		$map = array();
		$page_size = 10;

		$select_cate_parent = I('post.select_cate_parent',"");
		$p = I('post.p',1);
		if($p <= 0){
			$p = 1;
		}
		if($select_cate_parent == ""){
			$select_cate_parent = 0;
		}
		$this -> cate = array();
		if($select_cate_parent !== 0){
			$this -> queryChild($select_cate_parent);
			$map['cate_id'] = array('in',$this -> cate);
		}

		$page = array('curpage'=>$p,'size'=>$page_size);

		$result = apiCall(ProductApi::QUERY_ON_SHELF,array($map,$page));
		if($result['status']){
			foreach($result['info']['list'] as &$vo){
				$map = array(
					'p_id' => $vo['id'],
					'start_time' => array('elt',time()),
					'end_time' => array('egt',time())
				);
				$res = apiCall(ProductGroupApi::GET_INFO,array($map));
				if($res['status']){
					if($res['info'] == ""){
						$vo['able'] = 1;
					}else{
						$vo['able'] = 0;
					}
				}else{
					$this -> error($res['info']);
				}
			}
			$result['info']['p'] = $p;
			$result['info']['page'] = ceil($result['info']['count']/$page_size);
			echo json_encode($result);
		}else{
			$this -> error($result['info']);
		}

	}

	/**
	 * 废弃
	 * @param $group
	 * @return mixed
	 */
	private function getProductName($group){
		$arr = $group;
		foreach($group as $k=>$vo){
			$id = $vo['p_id'];
			$result = apiCall(ProductApi::GET_INFO,array(array('id' => $id)));
			if(!$result['status']){
				$this -> error($result['info']);
			}

			$arr[$k]['p_name'] = $result['info']['name'];
		}
		return $arr;
	}

	public function edit(){
		if(IS_POST){
			$p_id = I('post.p_id',0);
			$g_id = I('post.g_id',0);
			$price = I('post.price_'.$p_id,0,'floatval');
			$start_time = I('post.start_time_'.$p_id,'');
			$start_time = strtotime($start_time);
			$end_time = I('post.end_time_'.$p_id,'');
			$end_time = strtotime($end_time);
			$display_order = I('display_order_'.$p_id,0,'int');

			$entity = array(
				'price' => $price,
				'start_time' => $start_time,
				'end_time' => $end_time,
				'display_order' => $display_order
			);

			$map = array(
				'p_id' => $p_id,
				'g_id' => $g_id,
			);
			$result = apiCall(ProductGroupApi::SAVE,array($map,$entity));

			if($result['status']){
				$this -> success('修改成功！',U('Admin/ProductGroup/index',array('id'=>$g_id)));
			}else{
				$this -> error($result['info']);
			}

		}
	}

	public function delete(){
		$id = I('id',0);

		$result = apiCall(ProductGroupApi::DELETE,array(array('id'=>$id)));
		if($result['status']){
			$this -> success('删除成功！');
		}else{
			$this -> error($result['info']);
		}

	}

	public function add(){
		if(IS_POST){
			$g_id = I('post.g_id',0);
			$p_id = I('post.p_id',0);
			$entity = array(
				'p_id' => $p_id,
				'g_id' => $g_id,
			);
			$start_time = I('post.new_start_time','');
			$end_time = I('post.new_end_time','');
			$entity['start_time']=strtotime($start_time);
			$entity['end_time']=strtotime($end_time);
			$entity['price']= I('post.new_price',0,'floatval');
			$entity['display_order'] =  I('post.new_display_order',0,'int');
			$result = apiCall(ProductGroupApi::ADD, array($entity));
			if(!$result['status']){
				$this->error($result['info']);
			}

			$this->success('操作成功！',U('Admin/ProductGroup/index',array('id'=>$g_id)));
		}
	}

	/**
	 * 循环版本
	 * @param $id
	 * @return array
	 */
	private function queryChild($id){
		$que = array();
		array_push($que,$id);
		while(count($que)!=0){
			$tmp = array_shift($que);
			$map = array(
					'parent' => $tmp,
			);
			$result = apiCall(CategoryApi::QUERY_NO_PAGING,array(array($map)));

			if($result['status']){
				if(is_array($result['info'])){
					foreach($result['info'] as $val){
						array_push($que,$val['id']);
					}
				}else{
					array_push($this -> cate , $tmp );
				}
			}else{
				return array('status' => false,'info' => $result['info']);
			}
		}
		return array('status' => true,'info' => $this -> cate);
	}

	/*private function getTime($groups){

		$array = array();


		foreach($groups as $vo){
			$start_time = $vo['start_time'];
			$end_time = $vo['end_time'];
			if($start_time == 0){
				$start_time = time();
				$end_time = time()+3600*24*31;
			}
			$array[$vo['g_id']]=array(
					'start_time' => $start_time,
					'end_time' => $end_time,
					'price'=>$vo['price']
			);
		}

		return $array;
	}*/

	/**
	 *
	 */
	/*private function getGroups($groups){
		$str = "";
		foreach($groups as $vo){
			$str = $str.$vo['g_id'].',';
		}
		return $str;
	}*/



	/**
	 * 备份
	 */
	/*public function add(){
		$product_id = I('post.product_id',0);
		$groups = I('post.groups',array());
		if($product_id == 0){
			$this->error("参数错误！");
		}
        //1. 删除之前的分组
        $map['p_id'] = $product_id;
        $result = apiCall(ProductGroupApi::DELETE , array($map));

		foreach($groups as $key=>$group_id){
            $entity  = array(
                'p_id'=>$product_id,
                'g_id'=>$group_id,
            );
            $entity['start_time']=strtotime(I('post.start_time_'.$group_id,''));
            $entity['end_time']=strtotime(I('post.end_time_'.$group_id,''));
            $entity['price']= I('post.price_'.$group_id,0,'floatval');

            //2. 新增分组
			$result = apiCall(ProductGroupApi::ADD, array($entity));
			if(!$result['status']){
				$this->error($result['info']);
			}
		}
		
		if($result['status']){
			$this->success("操作成功！");
		}else{
			$this->error($result['info']);
		}
	}*/
	
}
